﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.Service.Shared;

namespace Bymed.SpineScan.Service
{
    public class BasicConfigurationService
    {
        /// <summary>
        /// Load BasicConfiguration instance from App.Config
        /// </summary>
        /// <returns></returns>
        public BasicConfiguration Load()
        {
            return new BasicConfiguration
            {
                PatientNumberPrefix = (string)ConfigFileUtility.GetValue(ConfigurationConstant.PatientNumberPrefix),
                PatientNumberContainsYear = bool.Parse((string)ConfigFileUtility.GetValue(ConfigurationConstant.PatientNumberContainsYear)),
                PatientNumberContainsMonth = bool.Parse((string)ConfigFileUtility.GetValue(ConfigurationConstant.PatientNumberContainsMonth)),
                PatientNumberContainsDay = bool.Parse((string)ConfigFileUtility.GetValue(ConfigurationConstant.PatientNumberContainsDay)),
                PatientNumberInitializedSequence = (string)ConfigFileUtility.GetValue(ConfigurationConstant.PatientNumberInitializedSequence),
                
                CompanyName = (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyName),
                CompanyLogoFileName = (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyLogoFileName),

                CompanyLogoFileFullName = Path.Combine(Path.Combine(Environment.CurrentDirectory, 
                (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyLogoDirectoryName)), 
                (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyLogoFileName)),

                CompanyAddress = (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyAddress),
                CompanyPhoneNumber = (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyPhoneNumber),
                TestReportTitle = (string)ConfigFileUtility.GetValue(ConfigurationConstant.TestReportTitle),
                DepartmentName = (string)ConfigFileUtility.GetValue(ConfigurationConstant.DepartmentName)
            };
        }

        /// <summary>
        /// Save provided BasicConfiguration instance to App.Config
        /// </summary>
        /// <param name="entity"></param>
        public void Save(BasicConfiguration entity)
        {
            entity.CompanyName = string.IsNullOrEmpty(entity.CompanyName) ? "上海毕迈信息科技" : entity.CompanyName.Trim();
            entity.CompanyAddress = string.IsNullOrEmpty(entity.CompanyAddress) ? "上海市肇家浜路768号瓯江大厦1801室" : entity.CompanyAddress.Trim();
            entity.CompanyPhoneNumber = string.IsNullOrEmpty(entity.CompanyPhoneNumber) ? "400-820-4799" : entity.CompanyPhoneNumber.Trim();
            entity.TestReportTitle = string.IsNullOrEmpty(entity.TestReportTitle) ? "脊柱健康测评报告" : entity.TestReportTitle.Trim();
            entity.DepartmentName = string.IsNullOrEmpty(entity.DepartmentName) ? "科室" : entity.DepartmentName.Trim();

            ConfigFileUtility.ChangeValue(ConfigurationConstant.PatientNumberPrefix, entity.PatientNumberPrefix);
            ConfigFileUtility.ChangeValue(ConfigurationConstant.PatientNumberContainsYear, entity.PatientNumberContainsYear.ToString());
            ConfigFileUtility.ChangeValue(ConfigurationConstant.PatientNumberContainsMonth, entity.PatientNumberContainsMonth.ToString());
            ConfigFileUtility.ChangeValue(ConfigurationConstant.PatientNumberContainsDay, entity.PatientNumberContainsDay.ToString());
            ConfigFileUtility.ChangeValue(ConfigurationConstant.PatientNumberInitializedSequence, entity.PatientNumberInitializedSequence);
            ConfigFileUtility.ChangeValue(ConfigurationConstant.CompanyName, entity.CompanyName);
            ConfigFileUtility.ChangeValue(ConfigurationConstant.CompanyAddress, entity.CompanyAddress);
            ConfigFileUtility.ChangeValue(ConfigurationConstant.CompanyPhoneNumber, entity.CompanyPhoneNumber);
            ConfigFileUtility.ChangeValue(ConfigurationConstant.TestReportTitle, entity.TestReportTitle);
            ConfigFileUtility.ChangeValue(ConfigurationConstant.DepartmentName, entity.DepartmentName);

            // Get the orginial logo file full name from App.Config again
            var originalCompanyLogoFileFullName = Path.Combine(Environment.CurrentDirectory, 
                (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyLogoFileName));

            // If the logo file is changed.
            if (originalCompanyLogoFileFullName != entity.CompanyLogoFileFullName)
            {
                // Get the log directory full name.
                var logoDirectoryFullName = Path.Combine(Environment.CurrentDirectory, (string)ConfigFileUtility.GetValue(ConfigurationConstant.CompanyLogoDirectoryName));

                // Check the logo directory
                if (!Directory.Exists(logoDirectoryFullName))
                {
                    Directory.CreateDirectory(logoDirectoryFullName);
                }

                // If selected logo file is not existing in logo directory
                if (!entity.CompanyLogoFileFullName.Contains(logoDirectoryFullName))
                {
                    File.Copy(entity.CompanyLogoFileFullName, Path.Combine(logoDirectoryFullName, entity.CompanyLogoFileName), true);
                }

                // Save the change
                ConfigFileUtility.ChangeValue(ConfigurationConstant.CompanyLogoFileName, entity.CompanyLogoFileName);
            }

            ConfigFileUtility.RefreshAppSettingsSection();
        }
    }
}
